package com.kexio.user.dto;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 租户信息DTO
 * 
 * 用于传输租户的基本信息，包括：
 * - 租户标识和名称
 * - 租户状态信息
 * - 审计信息
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class TenantInfo implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    // ==================== 基础字段 ====================
    
    /**
     * 租户ID
     */
    private String tenantId;
    
    /**
     * 租户名称
     */
    private String tenantName;
    
    /**
     * 租户描述
     */
    private String description;
    
    /**
     * 租户编码（可选，用于业务标识）
     */
    private String tenantCode;
    
    /**
     * 租户类型（如：ENTERPRISE-企业版，STANDARD-标准版，TRIAL-试用版）
     */
    private String tenantType;
    
    // ==================== 状态字段 ====================
    
    /**
     * 状态：1-启用，0-禁用
     */
    private Integer status;
    
    /**
     * 是否启用（便捷字段）
     */
    private Boolean enabled;
    
    /**
     * 是否已过期
     */
    private Boolean expired;
    
    /**
     * 过期时间
     */
    private LocalDateTime expiredAt;
    
    // ==================== 配额字段 ====================
    
    /**
     * 最大用户数量限制
     */
    private Integer maxUsers;
    
    /**
     * 当前用户数量
     */
    private Integer currentUsers;
    
    /**
     * 存储空间限制（MB）
     */
    private Long storageQuota;
    
    /**
     * 已使用存储空间（MB）
     */
    private Long usedStorage;
    
    // ==================== 联系信息 ====================
    
    /**
     * 联系人
     */
    private String contactName;
    
    /**
     * 联系电话
     */
    private String contactPhone;
    
    /**
     * 联系邮箱
     */
    private String contactEmail;
    
    /**
     * 租户地址
     */
    private String address;
    
    // ==================== 审计字段 ====================
    
    /**
     * 创建时间
     */
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    private LocalDateTime updatedAt;
    
    /**
     * 创建人
     */
    private String createdBy;
    
    /**
     * 更新人
     */
    private String updatedBy;
    
    // ==================== 配置字段 ====================
    
    /**
     * 租户配置（JSON格式）
     */
    private String tenantConfig;
    
    /**
     * 自定义属性（JSON格式）
     */
    private String customAttributes;
    
    // ==================== 构造函数 ====================
    
    public TenantInfo() {
    }
    
    // ==================== Getter/Setter ====================
    
    public String getTenantId() {
        return tenantId;
    }
    
    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }
    
    public String getTenantName() {
        return tenantName;
    }
    
    public void setTenantName(String tenantName) {
        this.tenantName = tenantName;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getTenantCode() {
        return tenantCode;
    }
    
    public void setTenantCode(String tenantCode) {
        this.tenantCode = tenantCode;
    }
    
    public String getTenantType() {
        return tenantType;
    }
    
    public void setTenantType(String tenantType) {
        this.tenantType = tenantType;
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public Boolean getEnabled() {
        return enabled;
    }
    
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    
    public Boolean getExpired() {
        return expired;
    }
    
    public void setExpired(Boolean expired) {
        this.expired = expired;
    }
    
    public LocalDateTime getExpiredAt() {
        return expiredAt;
    }
    
    public void setExpiredAt(LocalDateTime expiredAt) {
        this.expiredAt = expiredAt;
    }
    
    public Integer getMaxUsers() {
        return maxUsers;
    }
    
    public void setMaxUsers(Integer maxUsers) {
        this.maxUsers = maxUsers;
    }
    
    public Integer getCurrentUsers() {
        return currentUsers;
    }
    
    public void setCurrentUsers(Integer currentUsers) {
        this.currentUsers = currentUsers;
    }
    
    public Long getStorageQuota() {
        return storageQuota;
    }
    
    public void setStorageQuota(Long storageQuota) {
        this.storageQuota = storageQuota;
    }
    
    public Long getUsedStorage() {
        return usedStorage;
    }
    
    public void setUsedStorage(Long usedStorage) {
        this.usedStorage = usedStorage;
    }
    
    public String getContactName() {
        return contactName;
    }
    
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }
    
    public String getContactPhone() {
        return contactPhone;
    }
    
    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }
    
    public String getContactEmail() {
        return contactEmail;
    }
    
    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public LocalDateTime getCreateTime() {
        return createdAt;
    }
    
    public void setCreateTime(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdateTime() {
        return updatedAt;
    }
    
    public void setUpdateTime(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public String getCreateBy() {
        return createdBy;
    }
    
    public void setCreateBy(String createdBy) {
        this.createdBy = createdBy;
    }
    
    public String getUpdateBy() {
        return updatedBy;
    }
    
    public void setUpdateBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }
    
    public String getTenantConfig() {
        return tenantConfig;
    }
    
    public void setTenantConfig(String tenantConfig) {
        this.tenantConfig = tenantConfig;
    }
    
    public String getCustomAttributes() {
        return customAttributes;
    }
    
    public void setCustomAttributes(String customAttributes) {
        this.customAttributes = customAttributes;
    }
    
    // ==================== 便捷方法 ====================
    
    /**
     * 检查租户是否启用
     * 
     * @return true-启用，false-禁用或未设置
     */
    public boolean isEnabled() {
        return Boolean.TRUE.equals(this.enabled) && Integer.valueOf(1).equals(this.status);
    }
    
    /**
     * 检查租户是否已过期
     * 
     * @return true-已过期，false-未过期
     */
    public boolean isExpired() {
        if (Boolean.TRUE.equals(this.expired)) {
            return true;
        }
        
        if (this.expiredAt != null) {
            return this.expiredAt.isBefore(LocalDateTime.now());
        }
        
        return false;
    }
    
    /**
     * 检查租户是否可用（启用且未过期）
     * 
     * @return true-可用，false-不可用
     */
    public boolean isAvailable() {
        return isEnabled() && !isExpired();
    }
    
    /**
     * 计算用户使用率（百分比）
     * 
     * @return 用户使用率（0-100）
     */
    public double getUserUsagePercentage() {
        if (maxUsers == null || maxUsers <= 0 || currentUsers == null) {
            return 0.0;
        }
        return (currentUsers * 100.0) / maxUsers;
    }
    
    /**
     * 计算存储使用率（百分比）
     * 
     * @return 存储使用率（0-100）
     */
    public double getStorageUsagePercentage() {
        if (storageQuota == null || storageQuota <= 0 || usedStorage == null) {
            return 0.0;
        }
        return (usedStorage * 100.0) / storageQuota;
    }
    
    /**
     * 检查用户数量是否超限
     * 
     * @return true-超限，false-正常
     */
    public boolean isUserQuotaExceeded() {
        if (maxUsers == null || currentUsers == null) {
            return false;
        }
        return currentUsers > maxUsers;
    }
    
    /**
     * 检查存储空间是否超限
     * 
     * @return true-超限，false-正常
     */
    public boolean isStorageQuotaExceeded() {
        if (storageQuota == null || usedStorage == null) {
            return false;
        }
        return usedStorage > storageQuota;
    }
    
    @Override
    public String toString() {
        return "TenantInfo{" +
                "tenantId='" + tenantId + '\'' +
                ", tenantName='" + tenantName + '\'' +
                ", tenantCode='" + tenantCode + '\'' +
                ", tenantType='" + tenantType + '\'' +
                ", status=" + status +
                ", enabled=" + enabled +
                ", expired=" + expired +
                ", maxUsers=" + maxUsers +
                ", currentUsers=" + currentUsers +
                ", storageQuota=" + storageQuota +
                ", usedStorage=" + usedStorage +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}
